<template>
	<view class="fui-wrap">
		<fui-list>
			<block v-for="(item,index) in list" :key="index">
			<fui-list-cell arrow @click="clickItem(item,index)">
				<view class="cell">
					<view class="item">
						<view class="line">
							<fui-text text="停车券：" color="#afafaf" :size="28"></fui-text>
							<fui-text :text="item.title" color="#000" :size="28"></fui-text>
						</view>
						<view class="line">
							<fui-tag :text="item.coupon_type_txt" type="primary"></fui-tag>
						</view>
					</view>
					<view class="status">
						<fui-tag v-if="item.status=='normal'" text="正常" type="success"></fui-tag>
						<fui-tag v-if="item.status=='hidden'" text="禁用" type="danger"></fui-tag>
					</view>
				</view>
			</fui-list-cell>
			</block>
		</fui-list>
		<fui-fab @click="$navigateTo('parking/merch/coupon-setting-detail')" position="right" :bottom="30" width="70" distance="30">
			<fui-icon name="plus" color="#fff" size="60"></fui-icon>
		</fui-fab>
		<fui-modal v-if="buttons" :show="act.show" descr="请选择操作" :buttons="buttons" :radius="16" :maskClosable="true" direction="column" @click="clickButton" @cancel="cancelButton"></fui-modal>
		<fui-empty v-if="list.length===0" :src="`${baseUrl}assets/img/miniapp/empty.png`" title="暂无数据!" :marginTop="200"></fui-empty>
	</view>
</template>

<script>
	import {methods,baseUrl} from '@/utils/core.js';
	export default {
		data() {
			return {
				baseUrl:baseUrl,
				list:[],
				page:1,
				endData:false,
				act:{
					show:false,
					index:'',
					item:''
				},
				buttons:''
			}
		},
		onLoad(e) {
			this.getList();
		},
		onShow() {
			let refresh=uni.getStorageSync('refresh') || '';
			if(refresh){
				this.list=[];
				this.page=1;
				this.endData=false;
				uni.setStorageSync('refresh','');
				this.getList();
			}
		},
		onReachBottom() {
			this.getList();
		},
		onPullDownRefresh() {
			this.list=[];
			this.page=1;
			this.endData=false;
			this.getList(function(){
				uni.stopPullDownRefresh();
			});
		},
		methods: {
			...methods,
			clickItem:function(item,index){
				this.buttons=[
					{
						text: '编辑',
						background: '#22b2ff',
						value:'edit'
					},{
						text: (item.status=='normal')?'禁用':'启用',
						background: (item.status=='normal')?'#e59900':'#75ba1b',
						value:'status'
					},{
						text: '删除',
						background: '#ff0000',
						value:'delete'
					}
				];
				this.act={
					show:true,
					item:item,
					index:index
				};
			},
			clickButton:function(e){
				let id=this.act.item.id;
				let index=this.act.index;
				if(e.value=='edit'){
					this.$navigateTo('parking/merch/coupon-setting-detail?id='+id);
				}
				if(e.value=='status'){
					let status=(this.act.item.status=='normal')?'hidden':'normal';
					this.$post('parking/merch/change-coupon-status',{id:id,status:status},true,true).then(res=>{
						this.list[index].status=status;
					});
				}
				if(e.value=='delete'){
					uni.showModal({
						content:'您确定要删除该停车券吗？',
						success:(e)=>{
							if(e.confirm){
								this.$post('parking/merch/del-coupon',{id:id},true,true).then(res=>{
									this.list.splice(index,1);
								});
							}
						}
					})
				}
				this.cancelButton();
			},
			cancelButton:function(){
				this.buttons='';
				this.act={
					show:false,
					index:'',
					item:''
				};
			},
			getList:function(callback=false){
				this.showPopup=false;
				if(this.endData){
					this.$msg('已经到底了');
					return;
				}
				this.$get('parking/merch/coupon-setting-list',{
					page:this.page
				},true).then(res=>{
					this.list=this.list.concat(res);
					this.page++;
					if(res.length<10){
						this.endData=true;
					}	
					if(callback){
						callback();
					}
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
.cell{
	width:100%;
	display: flex;
	align-items: center;
	justify-content: space-between;
}
.item{
	width: 75%;
	.line{
		display: flex;
		height: 50rpx;
	}
}
.status{
	width: 25%;
	text-align: right;
}
</style>